<?php
/**
 * Created by PhpStorm.
 * User: tengwei
 * Date: 2018/11/16
 * Time: 10:17 AM
 */
namespace common\sources\write\bank;
use common\models\craftsman\BuyUserBankAccount;
use Yii;
class BankAccess implements IBankAccess
{
    public function addBdBank($param)
    {
        $res = Yii::$app->db->createCommand()->insert('buyuser_bank_account', [
            'buy_id' => $param['buy_id'],
            'real_name' => $param['real_name'],
            'cardnum' => $param['cardnum'],
            'bankname' => $param['bankname'],
            'pro_name' => $param['pro_name'],
            'pro_code' => $param['pro_code'],
            'city_name' => $param['city_name'],
            'city_code' => $param['city_code'],
            'bankbranch' => $param['bankbranch'],
            'idcard' => $param['idcard'],
            'bankphone' => $param['bankphone'],
            'update_time' => $param['update_time'],
            'create_time' => $param['create_time']
        ])->execute();
        return $res;
    }

    public function modifyBank($param){
        $sql = 'UPDATE ' . BuyUserBankAccount::tableName() . ' SET cardnum = :cardnum,bankname = :bankname,pro_name = :pro_name ,pro_code = :pro_code,city_name = :city_name,city_code = :city_code,bankbranch = :bankbranch,bankphone = :bankphone,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':cardnum' => $param['cardnum'],
                ':bankname' => $param['bankname'],
                ':pro_name' => $param['pro_name'],
                ':pro_code' => $param['pro_code'],
                ':city_name'=> $param['city_name'],
                ':city_code'=> $param['city_code'],
                ':bankbranch' => $param['bankbranch'],
                ':bankphone'=> $param['bankphone'],
                ':update_time' => time(),
                ':id' => $param['b_id']
            ])->execute();
    }

    //修改开户名
    public function ModifyBankName($data) {
        $bank = BuyUserBankAccount::findOne(['id' => $data['id']]);
        if ($bank) {
            $bank->real_name = $data['name'];
            if ($bank->save()) {
                return true;
            }
        }
        return false;
    }

    //用户账户审核
    public function AuditUserBank($data) {
        $bank = BuyUserBankAccount::findOne(['id' => $data['id']]);
        if ($bank) {
            $bank->status = $data['audit_status'];
            if (3 == $data['audit_status']) {//不通过
                $bank->reason = $data['reason'];
                $bank->refuse_plat = $data['refuse_plat'];
            } else {
                $bank->reason = '';
            }
            $bank->operator = $data['operator'];
            $bank->audit_operator = $data['audit_operator'];
            $bank->audit_time = time();
            if ($bank->save()) {
                return true;
            }
        }
        return false;
    }

    //判断卡号是否已经绑定
    public function existBank($data) {
        $exist = BuyUserBankAccount::find()
            ->select('id')
            ->where(['cardnum' => $data['cardno']])
            ->asArray()
            ->all();
        return $exist;
    }

    //修改银行卡号
    public function ModifyBankNum($data) {
        $bank = BuyUserBankAccount::findOne(['id' => $data['id']]);
        if ($bank) {
            $bank->cardnum = $data['cardno'];
            if ($bank->save()) {
                return true;
            }
        }
        return false;
    }
}